package cn.larry.tools.easypay.impl;

import cn.larry.tools.easypay.EasyPayService;
import cn.larry.tools.easypay.PayStateEnum;
import cn.larry.tools.easypay.bean.ChannelPayBean;
import cn.larry.tools.easypay.bean.EasyPayBean;
import cn.larry.tools.easypay.utils.EasppayUtil;
import cn.larry.tools.string.StringTool;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * Larry
 * 2021/8/24 23:12
 * 易生支付
 *
 * @Version 1.0
 */
@Slf4j
public class EasyPayServiceImpl implements EasyPayService {

    /**
     * 支付预下单
     *
     * @param easyPayBean
     * @param tradeamt    订单金额（分）
     * @param payId       交易请求流水号
     * @param openid      微信用户ID
     * @param reqIp       支付请求IP
     * @return 支付标识：prepayId
     */
    @Override
    public ChannelPayBean createPay(EasyPayBean easyPayBean, String tradeamt, String payId, String openid, String reqIp) throws Exception {
        if (easyPayBean == null || StringTool.isBlank(tradeamt)
                || StringTool.isBlank(payId) || StringTool.isBlank(openid))
            return null;
        EasppayUtil easppayUtil = new EasppayUtil(easyPayBean);

        JSONObject payJson = easppayUtil.pay(tradeamt, payId, openid, reqIp);
        ChannelPayBean channelPayBean = new ChannelPayBean();
        channelPayBean.setMerTraceId(payJson.getString("wtorderid"));
        channelPayBean.setPrepayId(payJson.getString("prepayid"));

        return channelPayBean;
    }

    /**
     * 交易查询
     *
     * @param easyPayBean
     * @param payId       交易请求流水号
     * @return
     */
    @Override
    public ChannelPayBean queryPay(EasyPayBean easyPayBean, String payId) throws Exception {
        if (easyPayBean == null || StringTool.isBlank(payId))
            return null;

        EasppayUtil easppayUtil = new EasppayUtil(easyPayBean);

        JSONObject queryJson = easppayUtil.query(payId);
        ChannelPayBean channelPayBean = new ChannelPayBean();

        String resultcode = queryJson.getString("resultcode");
        if ("00".equals(resultcode)) {
            channelPayBean.setPayStateEnum(PayStateEnum.PAY_STATE_11);
            channelPayBean.setChannelTraceId(queryJson.getString("wxtransactionid"));
            channelPayBean.setPayTime(queryJson.getString("wxtimeend"));
            channelPayBean.setMerTraceId(queryJson.getString("wtorderid"));
            channelPayBean.setPayId(queryJson.getString("tradetrace"));
        } else if ("06".equals(resultcode)) {
            channelPayBean.setPayStateEnum(PayStateEnum.PAY_STATE_40);
        } else if ("AA".equals(resultcode)) {
            channelPayBean.setPayStateEnum(PayStateEnum.PAY_STATE_01);
        } else {
            channelPayBean = null;
        }
        return channelPayBean;
    }

    /**
     * 当日交易撤销
     *
     * @param easyPayBean
     * @param oriPayId    原交易请求流水号
     * @return
     */
    @Override
    public ChannelPayBean cancelPay(EasyPayBean easyPayBean, String oriPayId) throws Exception {
        if (easyPayBean == null || StringTool.isBlank(oriPayId))
            return null;

        EasppayUtil easppayUtil = new EasppayUtil(easyPayBean);
        JSONObject cancelJson = easppayUtil.cancel(oriPayId, null);

        ChannelPayBean channelPayBean = new ChannelPayBean();
        String resultcode = cancelJson.getString("resultcode");
        if ("00".equals(resultcode)) {
            channelPayBean.setPayStateEnum(PayStateEnum.PAY_STATE_32);
        } else {
            channelPayBean = null;
        }
        return channelPayBean;
    }
}
